/**
 * 部门js
 */
var commiStatistics = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				commiStatistics.curPage = 1;
				commiStatistics.setPageChange(null);
				commiStatistics.search();
			}
		};
	},

	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			commiStatistics.curPage = 1;
			commiStatistics.setPageChange(null);
			commiStatistics.search();
		});
		
		$('#excel').on('click', function() { 
			var url = '../../../mgr/order/excel';
			if (typeof (pageIdx) != "undefined") {
				commiStatistics.curPage = pageIdx;
			}

			var id = $.trim($('#searchModForm input[name="id"]').val());
			var userName = $.trim($('#searchModForm input[name="userName"]').val());
			var financialName = $.trim($('#searchModForm input[name="financialName"]').val());
			var productId = $.trim($('#productId').val());
			var beginDate = $.trim($('#searchModForm input[name="beginDate"]').val());
			var endDate = $.trim($('#searchModForm input[name="endDate"]').val());
			
			var payStatus = $.trim($('#searchModForm select[name="payStatus"]').val());
			var company = $.trim($('#searchModForm select[name="company"]').val());
			location.href = url+"?id="+ id + "&userName=" + userName + "&financialName=" + financialName + "&productId=" + productId
							+"&beginDateStr=" + beginDate + "&endDateStr=" + endDate
							+"&userId=" + userId + "&payStatus=" + payStatus+"&company="+company;   
		});
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		
	 	var url = 'mgr/commCtrl/getList';

		if (typeof (pageIdx) != "undefined") {
			commiStatistics.curPage = pageIdx;
		}
		
		var params = jsUtils.serializeJson($('#searchModForm'));
		params.curPage = 1;
		params.pageSize = 999999;
		params.userId = userId;
		
		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / commiStatistics.pageSize > 0) ? Math
						.ceil(totalCount / commiStatistics.pageSize) : 1;
						
				pageHelper.init(pageTotal, totalCount, commiStatistics.search,
						delCurPage); // 翻页事件
			}else{
				alert(data.msg);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询交易记录
	search : function(pageIdx) {
		var url = 'mgr/commCtrl/getList';
		
		if (typeof (pageIdx) != "undefined") {
			commiStatistics.curPage = pageIdx;
		}
		
		var params = jsUtils.serializeJson($('#searchModForm'));
		params.curPage = commiStatistics.curPage;
		params.pageSize = commiStatistics.pageSize;
		params.userId = userId;

		if(params.userId  == '' || params.userId == null){
			alert("请重新登录后再进行尝试！");
			return false;
		}

		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';

			if (data.result != null && data.result.length > 0) {
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					tr += '<tr><td><input type="hidden" name="userId" value="' +singleObj.userId+'"/>'
							+ (i + 1)
							+ '</td><td>'
							+  singleObj.userChannelName
							+ '</td><td>'
							+ singleObj.userOrgName
							+ '</td><td>'
							+ singleObj.userRealName
							+ '</td><td>'
							+ singleObj.userPhone
							+ '</td><td>'
							+ singleObj.commTotal
							+ '</td><td>'
							+ singleObj.commiPay
							+ '</td><td>'
							+ singleObj.commiUnPay
							+ '</td><td>'
							+'</td></tr>';
						
				}
			} else {
				tr = '<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			commiStatistics.bindEventOtherBtn();
			jsUtils.controlFunctionBtn();
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {
		
	},

	init : function() {
		jsUtils.controlFunctionBtn();
		commiStatistics.setPageChange(null);
		commiStatistics.search();
		commiStatistics.startEvent();
		
		commiStatistics.bindEvent();
	}
};

$(document).ready(function() {
	commiStatistics.init();
});

//提供给外部刷新调用
function refashData() {
	commiStatistics.setPageChange(null);
	commiStatistics.search();
}

 


